package com.dein.music.controller;

import com.dein.music.model.Music;
import com.dein.music.service.MusicService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("music/")
public class MusicController {
	private MusicService musicService;

	@Autowired
	@Qualifier("musicService")
	public void setMusicService(MusicService musicService) {
		this.musicService = musicService;
	}

	@RequestMapping(value = "addMusic", method = RequestMethod.POST)
	public @ResponseBody
	Object addMusic(Music music) {
		Map<String, Object> map = new HashMap<String, Object>();
		boolean result = musicService.addMusic(music);
		if (result) {
			map.put("result", false);
			return map;
		}
		map.put("result", true);
		return map;
	}

	@RequestMapping(value = "deleteMusic", method = RequestMethod.POST)
	public @ResponseBody
	Object deleteMusic(@RequestParam(value = "musicId") Long musicId) {
		Map<String, Object> map = new HashMap<String, Object>();
		boolean result = musicService.deleteMusic(musicId);
		if (result) {
			map.put("result", false);
			return map;
		}
		map.put("result", true);
		return map;
	}

	@RequestMapping(value = "getMusicById", method = RequestMethod.POST)
	public @ResponseBody
	Object getMusicById(@RequestParam(value = "musicId") Long musicId) {

		Music music = musicService.getMusicById(musicId);

		return music;
	}

	@RequestMapping(value = "updateMusic", method = RequestMethod.POST)
	public @ResponseBody
	Object updateMusic(Music music) {
		Map<String, Object> map = new HashMap<String, Object>();
		boolean result = musicService.updateMusic(music);
		if (result) {
			map.put("result", false);
			return map;
		}
		map.put("result", true);
		return map;
	}

	@RequestMapping(value = "getMusicList")
	@ResponseBody
	public List<Music> getMusicList() {
		return musicService.getMusicList();
	}

}
